package profiterole.waffle;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map.Entry;

import profiterole.api.MapReduce;
import profiterole.api.Waffle;

public class WaffleBackend {

	/*
	 * Entry { key = "key" value = "value" }
	 */

	private WaffleBackend() {
		throw new AssertionError("can't be here");
	}

	@SuppressWarnings("unchecked")
	public static void doWaffleData(Waffle<?> waffle) {

		List<?> list = waffle.getSortedUnModifiableList();
		StringBuilder builder = new StringBuilder();

		Entry<String, Integer> current = null;

		for (Object element : list) {
			current = (Entry<String, Integer>) element;

			builder.append("Entry { \n key = \"").append(current.getKey())
					.append("\" \n").append(" value = \"")
					.append(current.getValue()).append("\" \n } \n");
		}

		try {
			BufferedWriter out = new BufferedWriter(new FileWriter("test.txt"));
			out.write(builder.toString());
			out.close();
		} catch (IOException e) {
			System.out.println("TODO exception ");

		}
	}

	public static void main(String[] args) {

		// String one = System.getProperty("user.dir") +"/OneFile/";
		Waffle<?> waffle = MapReduce.perform(
				new File(System.getProperty("user.dir") + "/MapJobs/"), null);
		System.out.println(waffle);
		WaffleBackend.doWaffleData(waffle);
	}
}
